<template>
  <div class="dialog">
    <el-dialog :title="'批次' + titleText" :visible.sync="showDialog" width="40%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="formData" :model="formData" :rules="rules" label-width="120px" class="demo-formData">
        <el-form-item label="品种名称:" prop="varietyId">
          <el-select class="w100p" filterable clearable v-model="formData.varietyId" allow-create>
            <el-option v-for="item of FishVarietyData" :key="item.fishVarietyId" :label="item.varietyName"
              :value="item.fishVarietyId" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注:" class="w100p">
          <el-input v-model="formData.remark" type="textarea" clearable maxlength="200" show-word-limit />
        </el-form-item>
        <el-form-item label="上传图片:" prop="pictureId">
          <UpLoadImage :limit="1" v-if="show" @getFileIdList="getFileIdList" :idList="fileIdList">
          </UpLoadImage>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('formData')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UpLoadImage from "@/components-production-management/UpLoadImage";
import uploadimage from "@/components/uploadimage/uploadimage.vue";
import demoApi from "@/api/document";
import DocumentApi from "@/api/document";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety"
import homeBaseApi from "@/api/basic-info-manage/homeBase";
import { blobToBase64 } from "@/utils/convert";

import fishSpeciesImgApi from "@/api/traceability/basicsInfo/fishSpeciesImg";

export default {
  name: '',
  components: { uploadimage, UpLoadImage },
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      showImg: false,
      showDialog: false,
      FishVarietyData: [],
      fileIdList:[],
      formData: {},
      baseImg: '',
      rules: {
        varietyId: [
          { required: true, message: "请选择品种", trigger: ["change", "blur"] }
        ],
        pictureId: [
          { required: true, message: "请上传图片", trigger: ["change", "blur"] }
        ],
      }
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
      this.queryFishVariety()
      v && (this.formData = {})
      this.baseImg = false
      this.$refs.UploadImage && this.$refs.UploadImage.clearImageUrl()
      this.$refs['formData'] && this.$refs['formData'].resetFields()
    },
    nowChanceStau(v) {
      if (v === 'add') {
        homeBaseApi.getBreedTypeNo().then(res => {
          this.formData = {
            staffName: sessionStorage.getItem('staffName'),
            breedTypeNo: res.data
          }
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject;
        this.fileIdList = this.editObject.pictureId ? [this.editObject.pictureId] : []
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    getFileIdList(data) {
      let fileIdList = data.fileIdList || [];
      if (fileIdList.length) {
        this.$set(this.formData, 'pictureId', fileIdList[0])
      }
    },
    //上传图片
    getimageurl(e) {
      let fd = new FormData();
      fd.append("file", e);
      demoApi.fileUpload(fd).then(res => {
        if (res.success) {
          this.formData.pictureId = res.data;
          this.showImg = false;
          this.$message.success("上传成功");
        } else this.$message.error(res.message);
      });
    },
    fileDownload(fileId) {
      fileId && DocumentApi.fileDownload({ fileId }).then(res => {
        blobToBase64(res.data).then(response => {
          this.baseImg = response;
          this.showImg = true;
        });
      })
    },
    //查询可选鱼类品种
    queryFishVariety() {
      FishVarietyApi.queryByCondition().then(res => {
        this.FishVarietyData = res.data;
      })
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            fishSpeciesImgApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            fishSpeciesImgApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .imgBox {
  width: 200px;
  height: 100px;

  .xunzeimage,
  .imgUrl {
    width: 100px;
    height: 100px;

    img,
    div {
      height: 100%;
      width: 100%;
    }

    .el-icon-close {
      color: #ffffff;
      transform: translate(15px, -15px);
    }
  }
}
</style>
